package com.potenza.training.resume.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.potenza.training.resume.dao.ResumePersonalSkillsDao;
import com.potenza.training.resume.entity.ResumePersonalSkills;
import com.potenza.training.resume.service.ResumePersonalSkillsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("resumePersonalSkillsServiceImpl")
public class ResumePersonalSkillsServiceImpl extends ServiceImpl<ResumePersonalSkillsDao, ResumePersonalSkills> implements ResumePersonalSkillsService {

    @Autowired
    private ResumePersonalSkillsDao resumePersonalSkillsDao;

    @Override
    public List<ResumePersonalSkills> findAll(Integer userId) {
        QueryWrapper<ResumePersonalSkills> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ResumePersonalSkills::getResumeId,userId);
        return resumePersonalSkillsDao.selectList(queryWrapper);
    }

    @Override
    public void saveResumePersonalSkills(Map<String, Object> requestMap) {
        List<Integer> skillsList =  (ArrayList) requestMap.get("skills");
        Integer resumeId = (Integer) requestMap.get("resumeId");
        QueryWrapper<ResumePersonalSkills> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ResumePersonalSkills::getResumeId,resumeId);
        resumePersonalSkillsDao.delete(queryWrapper);

        skillsList.forEach(integer -> {
            ResumePersonalSkills resumePersonalSkills = new ResumePersonalSkills();
            resumePersonalSkills.setSkills(integer);
            resumePersonalSkills.setResumeId(resumeId);
            resumePersonalSkillsDao.insert(resumePersonalSkills);
        });
    }
}